package com.tanhua.server.controller;

/**
 * @Author: cl
 * @Date: 2022/5/2 - 05 - 02
 * @Description:
 */

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.CountsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.tanhua.model.vo.UserInfoVo;

import java.io.IOException;
import java.util.Map;

/**
 * 用户信息管理
 */
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping()
    public ResponseEntity getUserInfo(Long userId) {

        //1、如果传过来的有id说明查询的是别人的信息，没有id就是自己的信息
        if (userId == null) {
            userId = UserHolder.getUserId();
        }

        //2、调用service
        UserInfoVo userInfo = userInfoService.findById(userId);

        //return ResponseEntity.ok().body(userInfo);  底层进行了简单封装
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 修改用户信息
     *
     * @param userInfo
     * @return
     */
    @PutMapping()
    public ResponseEntity update(@RequestBody UserInfo userInfo) {

        //获取用户的id并封装到实体类
        Long userId = UserHolder.getUserId();
        userInfo.setId(userId);
        //调用service进行修改
        userInfoService.update(userInfo);
        return ResponseEntity.ok("信息更新成功");
    }

    /**
     * 更新头像
     *
     * @param headPhoto
     * @return
     * @throws IOException
     */
    @PostMapping("/header")
    public ResponseEntity updateHeader(MultipartFile headPhoto) throws IOException {

        Long userId = UserHolder.getUserId();

        userInfoService.updateHead(headPhoto, userId);

        return ResponseEntity.ok("上传头像成功");
    }


    /**
     * 分页查询互相喜欢、喜欢、粉丝、谁看过我
     * type:1 互相关注、2 我关注、3 粉丝、4 谁看过我
     *
     * @param nickname
     * @param type
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity get(String nickname, @PathVariable Integer type,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pagesize) {

        PageResult pr = userInfoService.get(type, nickname, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 获取个人喜欢、粉丝、相互喜欢的个数
     *
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        CountsVo map = userInfoService.counts();
        return ResponseEntity.ok(map);
    }

    /**
     * 喜欢粉丝
     *
     * @param uid
     * @return
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fans(@PathVariable("uid") Long uid) {

        System.out.println(uid + "_-------------------------------------------");
        userInfoService.fans(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消喜欢
     *
     * @param uid
     * @return
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity unLike(@PathVariable Long uid) {

        System.out.println(uid + "_-------------------------------------------");
        userInfoService.unLike(Long.valueOf(uid));
        return ResponseEntity.ok(null);
    }

    /**
     * 是否喜欢
     *
     * @param uid
     * @return
     */
    @GetMapping("/{uid}/alreadyLove")
    public ResponseEntity alreadyLove(@PathVariable Long uid) {

       boolean b = userInfoService.alreadyLove(uid);
       return ResponseEntity.ok(b);
    }
}
